package com.ruoyi.mes.utils;

import com.google.gson.Gson;
import com.ruoyi.mes.vo.device.LongMenResponseVo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 龙门工具类 （功能比较局限）
 */
public class LongMenUtils {
    /**
     * 调取龙门进行取料（采取直接调用c#可执行文件）
     */
    public static void LongMenGrab(){
        try {
            // 指定C#可执行文件的路径
            String csharpExecutable = "E:/学习相关/MES系统/设备/龙门/ModbusTcp1.exe";

            // 构建进程并执行C#程序
            Process process = Runtime.getRuntime().exec(csharpExecutable);

            // 可以选择等待C#程序执行完成
            int exitCode = process.waitFor();

            // 处理程序的返回代码
            if (exitCode != 0) {
                System.out.println("C#程序执行成功！");
            } else {
                System.err.println("C#程序执行失败，返回代码: " + exitCode);
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调取龙门进行放
     */
    public static void LongMenPut(){
        try {
            // 指定C#可执行文件的路径
            String csharpExecutable = "E:/学习相关/MES系统/设备/龙门/ModbusTcp2.exe";

            // 构建进程并执行C#程序
            Process process = Runtime.getRuntime().exec(csharpExecutable);

            // 可以选择等待C#程序执行完成
            int exitCode = process.waitFor();

            // 处理程序的返回代码
            if (exitCode != 0) {
                System.out.println("C#程序执行成功！");
            } else {
                System.err.println("C#程序执行失败，返回代码: " + exitCode);
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 龙门根据库位进行放料
     * @param locatorInfoId
     * @return
     */
    public static LongMenResponseVo LongMenPutWithLocator(Long locatorInfoId) {
        int number = locatorInfoId.intValue() -1;
        // API地址
        String url = "http://192.168.1.104:5002/api/longmen";
        // 请求数据
        String requestData = "{\"output\":\"放料\",\"number\":" + number + "}";

        LongMenResponseVo longMenResponseVo = null;
        try {
            // 创建URL对象
            URL apiUrl = new URL(url);
            // 打开URL连接
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求头部信息
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            // 允许输出流写入数据
            connection.setDoOutput(true);
            // 将请求数据写入输出流
            connection.getOutputStream().write(requestData.getBytes("UTF-8"));
            // 发送HTTP请求
            connection.connect();

            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 关闭连接
            connection.disconnect();

            // 解析JSON数据并存储在对象中
            String responseData = response.toString();
            Gson gson = new Gson();
            longMenResponseVo = gson.fromJson(responseData, LongMenResponseVo.class);
//            System.out.println(longMenResponseVo);

            // 断开连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return longMenResponseVo;
    }

    /**
     * 龙门根据库位进行取料
     * @param locatorInfoId
     * @return
     */
    public static LongMenResponseVo LongMenGrabWithLocator(Long locatorInfoId){
        int number = locatorInfoId.intValue() -1;
        // API地址
        String url = "http://192.168.1.104:5002/api/longmen";
        // 请求数据
        String requestData = "{\"shuru\":\"取料\",\"number\":" + number + "}";

        LongMenResponseVo longMenResponseVo = null;
        try {
            // 创建URL对象
            URL apiUrl = new URL(url);
            // 打开URL连接
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求头部信息
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            // 允许输出流写入数据
            connection.setDoOutput(true);
            // 将请求数据写入输出流
            connection.getOutputStream().write(requestData.getBytes("UTF-8"));
            // 发送HTTP请求
            connection.connect();

            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 关闭连接
            connection.disconnect();

            // 解析JSON数据并存储在对象中
            String responseData = response.toString();
            Gson gson = new Gson();
            longMenResponseVo = gson.fromJson(responseData, LongMenResponseVo.class);
//            System.out.println(longMenResponseVo);

            // 断开连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return longMenResponseVo;
    }
}
